<?php

class status_IndexController extends Zend_Controller_Action
{
  function init() {
	$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/ressources/status/style.css');
	
	$this->view->avatarBaseURL = Zend_Registry::get('app_config')->avatar->baseURL;
	
	//for the home we may return the response in JSOn format	
	$contextSwitch = $this->_helper->getHelper('contextSwitch');
	$contextSwitch->addActionContext('index', 'json');
	$contextSwitch->addActionContext('public', 'json');
	$contextSwitch->addActionContext('user', 'json');
	$contextSwitch->addActionContext('mention', 'json');
	$contextSwitch->addActionContext('tag', 'json');
	$contextSwitch->addActionContext('tagsearch', 'json');
	$contextSwitch->addActionContext('pseudosearch', 'json');
	$contextSwitch->initContext();
	
  }

  //we display the home of the authenticated user
  function indexAction() {    
    
	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$beforeStatusId = $this->_getParam('before');
	
	if(!empty($beforeStatusId)) $params['beforeStatusId'] = $beforeStatusId;
	
	$data = $engine->query('status','timeline','home',$params);

	//stream data for the user
	$this->view->stream = unserialize($data);	
	
	if($this->view->stream['status'] == 'KO') {
		$this->view->messageDisplay()->setMessage($this->view->stream['error']['message'],'error');
	}
	
	//If we are in JSONMode, we just need to format the statuses and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$stream = unserialize($data);
		$statuses = @$stream['data'];		
		if(empty($statuses)) {
			$this->view->nbStatuses= 0;
		}
		else {
			$this->view->nbStatuses= count($statuses);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($statuses as $mun=> $status) {

				$htmlString .= $this->view->partial('partialstatus.phtml', array(
					'status' => $status,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach status
			$this->view->htmlString = $htmlString;
		} //end statuses not empty
		return;
	} //end json mode
	
	//we provide the authenticated user id to check if he can delete the status
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
	

	
	
	//We save the details of the user in the session (for the links)
	$statusSession = new Zend_Session_Namespace('status');
	$userFollow = $statusSession->users;
	
	$userFollow[$this->view->stream['user']['pseudo']]['followers'] = $this->view->stream['follow']['nbFollowers'];
	$userFollow[$this->view->stream['user']['pseudo']]['following'] = $this->view->stream['follow']['nbFollowing'];
	
	$statusSession->users = $userFollow;



	
	$this->view->mode = 'home';
 	$this->_helper->viewRenderer('statuslist');
	
	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;	
	
	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/infiniteScrolling.js', 'text/javascript');
	
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/status/index/index/format/json/before/'); 

	//Add javascript for autocomplete of tags and pseudo
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
	$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
	$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');	
	
	
	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();
	
  } //end IndexAction

  //We display the user posted
  function userAction() {
    //Selection of the user, if a user id is provided, we look for this user
	$params = array();
	
	$userPseudo = $this->_getParam('pseudo');  	
	if(empty($userPseudo)) echo 'erreur 404';
	
	$params['userPseudo'] = $userPseudo;
	
	$beforeStatusId = $this->_getParam('before');
	if(!empty($beforeStatusId)) $params['beforeStatusId'] = $beforeStatusId;
	
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','timeline','user',$params);	

	//If we are in JSONMode, we just need to format the statuses and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$stream = unserialize($data);
		$statuses = @$stream['data'];		
		if(empty($statuses)) {
			$this->view->nbStatuses= 0;
		}
		else {
			$this->view->nbStatuses= count($statuses);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($statuses as $mun=> $status) {

				$htmlString .= $this->view->partial('partialstatus.phtml', array(
					'status' => $status,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach status
			$this->view->htmlString = $htmlString;
		} //end statuses not empty
		return;
	} //end json mode	

	
	//stream data for the user
	$this->view->stream = unserialize($data);

	//we provide the authenticated user pseudo to check if he is not displaying its page
	$this->view->userPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];

	$this->view->mode = 'statuses';
	$this->view->titleData =  htmlspecialchars($userPseudo);
 	$this->_helper->viewRenderer('statuslist');

	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;

	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/infiniteScrolling.js', 'text/javascript');
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/status/index/user/format/json/pseudo/'.$userPseudo.'/before/'); 

	//Add javascript for autocomplete of tags and pseudo
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
	$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
	$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');	



	//We save the details of the user in the session (for the links)
	//We remove other users not anymore here
	$statusSession = new Zend_Session_Namespace('status');
	$userFollow = $statusSession->users;
	
	$viewingUserPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];
		
	//We keep the details of the viewing user
	$newUserFollow[$viewingUserPseudo] = $userFollow[$viewingUserPseudo];
	
	//We add the ones of the user being displayed
	$newUserFollow[$this->view->stream['user']['pseudo']]['followers'] = $this->view->stream['follow']['nbFollowers'];
	$newUserFollow[$this->view->stream['user']['pseudo']]['following'] = $this->view->stream['follow']['nbFollowing'];
	
	$statusSession->users = $newUserFollow;
	
	

	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();	
  }//end user action
  

  
  
  
  
  
  
  //We display the user posted
  function mentionAction() {
    //Selection of the user, if a user id is provided, we look for this user
	$params = array();
	
	$userPseudo = $this->_getParam('pseudo');  	
	if(empty($userPseudo)) echo 'erreur 404';
	
	$params['userPseudo'] = $userPseudo;
	
	$beforeStatusId = $this->_getParam('before');
	if(!empty($beforeStatusId)) $params['beforeStatusId'] = $beforeStatusId; 	
	
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','timeline','mention',$params);	

	
	//If we are in JSONMode, we just need to format the statuses and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$stream = unserialize($data);
		$statuses = @$stream['data'];		
		if(empty($statuses)) {
			$this->view->nbStatuses= 0;
		}
		else {
			$this->view->nbStatuses= count($statuses);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($statuses as $mun=> $status) {

				$htmlString .= $this->view->partial('partialstatus.phtml', array(
					'status' => $status,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach status
			$this->view->htmlString = $htmlString;
		} //end statuses not empty
		return;
	} //end json mode	
	
	
	
	//we provide the authenticated user pseudo to check if he is not displaying its page
	$this->view->userPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];
	
	//stream data for the user
	$this->view->stream = unserialize($data);
	$this->view->mode = 'mention';
	$this->view->titleData =  htmlspecialchars($userPseudo);
 	$this->_helper->viewRenderer('statuslist');

	
	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;

	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/infiniteScrolling.js', 'text/javascript');
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/status/index/mention/format/json/pseudo/'.$userPseudo.'/before/'); 
	
	//Add javascript for autocomplete of tags and pseudo
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
	$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
	$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');		

	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();	
  }//end mention action  

  



  //We display the user posted
  function publicAction() {
    //Selection of the user, if a user id is provided, we look for this user
	$params = array();
	
	$beforeStatusId = $this->_getParam('before');
	if(!empty($beforeStatusId)) $params['beforeStatusId'] = $beforeStatusId; 	
	
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','timeline','public',$params);	

	
	//If we are in JSONMode, we just need to format the statuses and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$stream = unserialize($data);
		$statuses = @$stream['data'];		
		if(empty($statuses)) {
			$this->view->nbStatuses= 0;
		}
		else {
			$this->view->nbStatuses= count($statuses);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];
			foreach($statuses as $mun=> $status) {

				$htmlString .= $this->view->partial('partialstatus.phtml', array(
					'status' => $status,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId 
					)
				);
			} //end foreach status
			$this->view->htmlString = $htmlString;
		} //end statuses not empty
		return;
	} //end json mode	
	
	
	
	//we provide the authenticated user pseudo to check if he is not displaying its page
	$this->view->userPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];
	
	//stream data for the user
	$this->view->stream = unserialize($data);
	$this->view->mode = 'public';
 	$this->_helper->viewRenderer('statuslist');

	//some functionality depends on the fact that the user is displaying its own page
	$this->view->authUserOwnPage = false;

	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/infiniteScrolling.js', 'text/javascript');
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/status/index/public/format/json/before/'); 
	
	//Add javascript for autocomplete of tags and pseudo
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
	$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
	$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');		

	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();	
  }//end mention action   
  




	function searchAction() {
		$engine = Myzen_Oauth_Query_Singleton::getInstance();
		$params = array('q' => $this->_getParam('q'));  
	    $data = $engine->query('status','search','index',$params);			

		$this->view->stream = unserialize($data);
		
		if($this->view->stream['status'] == 'KO') $this->view->messageDisplay()->setMessage($this->view->stream['error']['message'],'error');
				
		$this->view->mode = 'search';
		$this->view->titleData = htmlspecialchars($this->_getParam('q'));
		
	 	$this->_helper->viewRenderer('statuslist');
		$this->view->authUserOwnPage = false;

		//Add javascript for autocomplete of tags and pseudo
		$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
		$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
		$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
		$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');	

		$this->saveSidebarLinks();
		$this->saveBreadcrumbs();

	}  
  
  
  
    //We display the user posted
  function tagAction() {
    //Selection of the user, if a user id is provided, we look for this user
	$params = array();
	
	$tagId = $this->_getParam('tag');  	
	if(empty($tagId)) echo 'erreur 404';
	
	$params['tagId'] = $tagId;
	
	$beforeStatusId = $this->_getParam('before');
	if(!empty($beforeStatusId)) $params['beforeStatusId'] = $beforeStatusId; 	
	
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','timeline','tag',$params);	
	
	

	
	//If we are in JSONMode, we just need to format the statuses and send them back
	if($this->_helper->getHelper('contextSwitch')->getCurrentContext() == 'json') {
		$stream = unserialize($data);
		$statuses = @$stream['data'];		
		if(empty($statuses)) {
			$this->view->nbStatuses= 0;
		}
		else {
			$this->view->nbStatuses= count($statuses);
			$htmlString = '';
			$userId = Zend_Registry::get('app_session')->userDetails['id'];	
			foreach($statuses as $mun=> $status) {

				$htmlString .= $this->view->partial('partialstatus.phtml', array(
					'status' => $status,
					'avatarBaseURL' => $this->view->avatarBaseURL,
					'userId' => $userId
					)
				);
			} //end foreach status
			$this->view->htmlString = $htmlString;
		} //end statuses not empty
		return;
	} //end json mode	
	
	
	$this->view->userId = Zend_Registry::get('app_session')->userDetails['id'];
	
	//we provide the authenticated user pseudo to check if he is not displaying its page
	$this->view->userPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];
	
	//stream data for the user
	$this->view->stream = unserialize($data);
	$this->view->mode = 'tag';
 	$this->_helper->viewRenderer('statuslist');

	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;

	//Add javascript for infinite scrolling
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/infiniteScrolling.js', 'text/javascript');
	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
	$this->view->javascriptData('infiniteScrollBaseUrl',$this->view->baseURL().'/status/index/tag/format/json/tag/'.$tagId.'/before/'); 
	
	//Add javascript for autocomplete of tags and pseudo
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/status/javascript/autocompleteTagPseudo.js', 'text/javascript');
	$this->view->javascriptData('autocompleteTagBaseUrl',$this->view->baseURL().'/status/index/tagsearch/format/json/');
	$this->view->javascriptData('autocompletePseudoBaseUrl',$this->view->baseURL().'/status/index/pseudosearch/format/json/');		


	$this->saveSidebarLinks();	
	$this->saveBreadcrumbs();
  }//end tag action  
  
  
  function tagsearchAction() {
  	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$params['q'] = $this->_getParam('q');
	$params['where'] = 'begin';
	$params['perPage'] = 5;
	
	$data = $engine->query('status','tag','search',$params);
	$this->view->data = unserialize($data);
	
  }
  
  
  function pseudosearchAction() {
  	//We get the home data of the user
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    
	$params = array();
	$params['q'] = $this->_getParam('q');
	$params['where'] = 'begin';
	$params['perPage'] = 5;
	
	$data = $engine->query('user','search','bypseudo',$params);
	$this->view->data = unserialize($data);
	
  }  
  
  
  
  
  
  
  
    
  function followerAction() {
  	$userPseudo = $this->_getParam('pseudo');
  	    
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','friendship','list',array('userPseudo' => $userPseudo,'mode' => 'follower'));  	

	$this->view->stream = unserialize($data);
	$this->view->mode = 'follower';
	  
 	$this->_helper->viewRenderer('userlist');
 	
 	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;
 	
	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();
  } //end follower





  function followingAction() {
  	$userPseudo = $this->_getParam('pseudo');
  	    
	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('status','friendship','list',array('userPseudo' => $userPseudo,'mode' => 'following'));  	

	$this->view->stream = unserialize($data);

 	$this->_helper->viewRenderer('userlist');	  
	
	//we tell the view which mode we are in
	$this->view->mode = 'following';
	
	 	
 	//some functionality depends on the fact that the user is displaying its own page
	if($this->view->stream['user']['pseudo'] == Zend_Registry::get('app_session')->userDetails['pseudo']) $this->view->authUserOwnPage = true;
	else $this->view->authUserOwnPage = false;
	
	$this->saveSidebarLinks();
	$this->saveBreadcrumbs();
  } //end following





  function saveSidebarLinks() {

$statusSession = new Zend_Session_Namespace('status');

	$links = array();
	$users = $statusSession->users;
	$num = 0;
	
	$viewingUserPseudo = Zend_Registry::get('app_session')->userDetails['pseudo'];


	$links[$num] = array('text' => 'public', 'url' => '/status/public/');
	if($this->view->mode == 'public') $links[$num]['selected'] = 1;			
	$num++;

	
	foreach($users as $pseudo => $data) {
			$links[$num] = array('text' => $pseudo, 'url' => '/status/user/'.$pseudo.'/');
			
			if($pseudo == $viewingUserPseudo) $links[$num]['text'] = 'You';
			if($pseudo == $this->_getParam('pseudo') && $this->view->mode == 'statuses') $links[$num]['selected'] = 1;
			$num++;

			if($pseudo == $viewingUserPseudo) {
				
				$links[$num] = array('text' => 'Home', 'url' => '/status/','indent' => 1);	
				if($this->view->mode == 'home') $links[$num]['selected'] = 1; 
				$num++;
			}

			
			$links[$num] = array('text' => 'Followers('.$data['followers'].')', 'url' => '/status/user/'.$pseudo.'/follower/','indent' => 1);
			if($pseudo == $this->_getParam('pseudo') && $this->view->mode == 'follower') $links[$num]['selected'] = 1;			
			$num++;
			
			$links[$num] = array('text' => 'Following('.$data['following'].')', 'url' => '/status/user/'.$pseudo.'/following/','indent' => 1);
			if($pseudo == $this->_getParam('pseudo') && $this->view->mode == 'following') $links[$num]['selected'] = 1;
			$num++;
			
			$links[$num] = array('text' => 'Mentions', 'url' => '/status/mention/'.$pseudo.'/','indent' => 1);
			if($pseudo == $this->_getParam('pseudo') && $this->view->mode == 'mention') $links[$num]['selected'] = 1;
			$num++;



	}
	
	if(	$this->view->mode == 'tag') {
			$links[] = array('text' => 'Tag: '.$this->_getParam('tag'), 'url' => '/status/tag/'.$this->_getParam('tag').'/','selected'=> 1);		
	}
	
	
		
	$this->view->sidebarLinks($links);	
  }
  
  
  
  function saveBreadcrumbs() {
  	$crumbs = array(
		0 => array(
			'text' => 'Status', 
			'url'  => '/status/'
			));
	
	if($this->view->mode == 'public') $crumbs[1] = array('text'=> 'public'); 	
	if($this->view->mode == 'home') $crumbs[1] = array('text'=> 'home'); 
	
	if($this->view->mode == 'statuses') $crumbs[1] = array('text'=> $this->_getParam('pseudo'));
	
	if($this->view->mode == 'following') {
		$crumbs[1] = array('text'=> $this->_getParam('pseudo'),'url' => '/status/user/'.$this->_getParam('pseudo').'/');
		$crumbs[2] = array('text' => 'following');
	} 	

	if($this->view->mode == 'follower') {
		$crumbs[1] = array('text'=> $this->_getParam('pseudo'),'url' => '/status/user/'.$this->_getParam('pseudo').'/');
		$crumbs[2] = array('text' => 'follower');
	} 

	if($this->view->mode == 'mention') {
		$crumbs[1] = array('text'=> $this->_getParam('pseudo'),'url' => '/status/user/'.$this->_getParam('pseudo').'/');
		$crumbs[2] = array('text' => 'mention');
	} 

	if($this->view->mode == 'search') {
		$crumbs[1] = array('text'=> 'search','url' => '/status/search/');
		$crumbs[2] = array('text' => $this->view->titleData);
	} 
					

	if($this->view->mode == 'tag') {
		$crumbs[1] = array('text' => 'tag: '.$this->_getParam('tag'));
	} 
		
	
	$this->view->Breadcrumbs($crumbs);
	
		
  }




} //End class